package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.edu.domain.chapter.ChapterVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edu.domain.vo.EduChapterVo;
import com.ruoyi.edu.domain.bo.EduChapterBo;
import com.ruoyi.edu.service.IEduChapterService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程章节管理
 *
 * @author ruoyi
 * @date 2022-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@Anonymous
@RequestMapping("/edu/chapter")
public class EduChapterController extends BaseController {

    private final IEduChapterService iEduChapterService;
    @Anonymous
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = iEduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok("allChapterVideo",list);
    }
    /**
     * 查询课程章节管理列表
     */
    @SaCheckPermission("edu:chapter:list")
    @GetMapping("/list")
    public TableDataInfo<EduChapterVo> list(EduChapterBo bo, PageQuery pageQuery) {
        return iEduChapterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出课程章节管理列表
     */
    @SaCheckPermission("edu:chapter:export")
    @Log(title = "课程章节管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EduChapterBo bo, HttpServletResponse response) {
        List<EduChapterVo> list = iEduChapterService.queryList(bo);
        ExcelUtil.exportExcel(list, "课程章节管理", EduChapterVo.class, response);
    }

    /**
     * 获取课程章节管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("edu:chapter:query")
    @GetMapping("/{id}")
    public R<EduChapterVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iEduChapterService.queryById(id));
    }



    /**
     * 新增课程章节管理
     */
    @SaCheckPermission("edu:chapter:add")
    @Log(title = "课程章节管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EduChapterBo bo) {
        return toAjax(iEduChapterService.insertByBo(bo) ? 1 : 0);
    }
//    @SaCheckPermission("edu:chapter:add")
//    @Log(title = "课程章节管理", businessType = BusinessType.INSERT)
//    @RepeatSubmit()
//    @PutMapping("/put")
//    public R<Void> add(@Validated(AddGroup.class) @RequestBody EduChapterBo bo) {
//        return toAjax(iEduChapterService.insertByBo(bo) ? 1 : 0);
//    }

    /**
     * 修改课程章节管理
     */
    @SaCheckPermission("edu:chapter:edit")
    @Log(title = "课程章节管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EduChapterBo bo) {
        return toAjax(iEduChapterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除课程章节管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("edu:chapter:remove")
    @Log(title = "课程章节管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iEduChapterService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
//    public R remove(@PathVariable String chapterId){
//        boolean flag = iEduChapterService.deleteChapter(chapterId);
//        if (flag){
//            return R.ok();
//        }else {
//            return R.fail();
//        }
//    }
}
